import 'package:flame/game.dart';
import 'package:flutter/material.dart';

import 'GameWrapper.dart';
import 'main.dart';

class GameOverOverlay extends StatelessWidget {
  final Game game;

  const GameOverOverlay(this.game, {super.key});

  @override
  Widget build(BuildContext context) {
    // 获取GameWrapper组件以访问游戏状态和得分
    GameWrapper? gameWrapper;
    MyGame? myGame;
    if (game is FlameGame) {
      final flameGame = game as FlameGame;
      gameWrapper = flameGame.children.whereType<GameWrapper>().firstOrNull;
      myGame = flameGame as MyGame?;
    }
    
    // 如果无法获取GameWrapper，则使用默认值
    final score = gameWrapper?.getScore() ?? 0;
    final targetScore = gameWrapper?.getTargetScore() ?? 500;
    final isWin = score >= targetScore;
    final currentTime = gameWrapper?.getCurrentTime() ?? 0;
    final isTimeUp = currentTime <= 0;

    String title;
    if (isWin) {
      title = '恭喜成功！';
    } else if (isTimeUp) {
      title = '时间结束!';
    } else {
      title = '游戏结束';
    }

    return Material(
      color: Colors.black54,
      child: Center(
        child: Container(
          padding: const EdgeInsets.all(20),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(10),
          ),
          width: 300,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                title,
                style: const TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(height: 20),
              Text('最终得分: $score'),
              const SizedBox(height: 20),
              Text('目标得分: $targetScore'),
              const SizedBox(height: 20),
              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  onPressed: () {
                    // 重新开始当前关卡
                    if (gameWrapper != null && myGame != null) {
                      gameWrapper.resetScore();
                      gameWrapper.resetTimer();
                      // 重新初始化游戏板
                      myGame.restartGame();
                      gameWrapper.resumeTimer();
                    }
                    game.overlays.remove('gameOver');
                  },
                  child: const Text('重新开始'),
                ),
              ),
              const SizedBox(height: 10),
              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  onPressed: () {
                    // 下一关逻辑
                    if (gameWrapper != null && myGame != null) {
                      gameWrapper.resetScore();
                      gameWrapper.resetTimer();
                      // 进入下一关
                      myGame.nextLevel();
                      gameWrapper.resumeTimer();
                    }
                    game.overlays.remove('gameOver');
                  },
                  child: const Text('下一关'),
                ),
              ),
              const SizedBox(height: 10),
              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  onPressed: () {
                    // 回到首页
                    game.overlays.remove('gameOver');
                    // 这里可以添加更多回到首页的逻辑
                  },
                  child: const Text('回到首页'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}